Code
# Setup device------------------------------------------------
from BayesForge import bf
import jax.numpy as jnp
# Setup device------------------------------------------------
m = bf(platform='cpu', rand_seed = False)
# Simulate data ------------------------------------------------
N = 50
individual_predictor = m.dist.normal(0,1, shape = (N,1), sample = True)
kinship = m.dist.bernoulli(0.3, shape = (N,N), sample = True)
kinship = kinship.at[jnp.diag_indices(N)].set(0)
category = m.dist.categorical(jnp.array([.25,.25,.25,.25]), sample = True, shape = (N,))
N_grp, N_by_grp = jnp.unique(category, return_counts=True)
N_grp = N_grp.shape[0]
def sim_network(kinship, individual_predictor,category):
# Intercept
B_intercept = m.net.block_model(jnp.full((N,),0), 1, jnp.array([N]), sample = True)
B_category = m.net.block_model(category, N_grp, N_by_grp, sample = True)
# SR
sr = m.net.sender_receiver(
individual_predictor,
individual_predictor,
s_mu = 0.4, r_mu = -0.4, sample = True)
# D
DR = m.net.dyadic_effect(kinship, d_sd=2.5, sample = True)
return m.dist.bernoulli(
logits = B_intercept + B_category + sr + DR,
sample = True
)
network = sim_network(m.net.mat_to_edgl(kinship), individual_predictor, category)
# Predictive model ------------------------------------------------
m.data_on_model = dict(
network = network,
dyadic_predictors = m.net.mat_to_edgl(kinship),
focal_individual_predictors = individual_predictor,
target_individual_predictors = individual_predictor,
category = category
)
def model(network, dyadic_predictors, focal_individual_predictors, target_individual_predictors,category):
N_id = focal_individual_predictors.shape[0]
# Block ---------------------------------------
B_intercept = m.net.block_model(jnp.full((N_id,),0), 1, jnp.array([N_id]), name = "B_intercept")
B_category = m.net.block_model(category, N_grp, N_by_grp, name = "B_category")
## SR shape = N individuals---------------------------------------
sr = m.net.sender_receiver(
focal_individual_predictors,
target_individual_predictors,
s_mu = 0.4, r_mu = -0.4
)
# Dyadic shape = N dyads--------------------------------------
dr = m.net.dyadic_effect(dyadic_predictors, d_sd=2.5) # Diadic effect intercept only
m.dist.bernoulli(logits = B_intercept + B_category + sr + dr, obs=network)
m.fit(model, progress_bar=False)
m.summary()An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
bf v 0.0.60 package loaded
jax.local_device_count(cpu) = 32
| mean | sd | eti89_lb | eti89_ub | ess_bulk | ess_tail | r_hat | mcse_mean | mcse_sd | |
|---|---|---|---|---|---|---|---|---|---|
| b_B_category[0, 0] | -2.32 | 1.40 | -4.56 | -0.08 | 3580.87 | 3117.57 | 1.0 | 0.02 | 0.02 |
| b_B_category[0, 1] | -7.62 | 1.93 | -10.86 | -4.61 | 6283.29 | 3010.76 | 1.0 | 0.02 | 0.03 |
| b_B_category[0, 2] | -6.94 | 2.10 | -10.45 | -3.77 | 6356.50 | 2884.40 | 1.0 | 0.03 | 0.04 |
| b_B_category[0, 3] | -6.86 | 2.03 | -10.22 | -3.78 | 6916.25 | 3361.92 | 1.0 | 0.02 | 0.03 |
| b_B_category[1, 0] | -4.35 | 1.28 | -6.47 | -2.39 | 3040.57 | 3119.18 | 1.0 | 0.02 | 0.02 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| sr_rf[48, 1] | 0.19 | 0.82 | -0.96 | 1.59 | 5247.64 | 3280.13 | 1.0 | 0.01 | 0.02 |
| sr_rf[49, 0] | -1.57 | 2.49 | -5.84 | 1.98 | 4667.77 | 2919.79 | 1.0 | 0.04 | 0.04 |
| sr_rf[49, 1] | 0.19 | 0.77 | -0.90 | 1.50 | 4878.69 | 3026.32 | 1.0 | 0.01 | 0.02 |
| sr_sigma[0] | 2.99 | 0.77 | 1.87 | 4.33 | 1476.25 | 1781.99 | 1.0 | 0.02 | 0.01 |
| sr_sigma[1] | 0.78 | 0.54 | 0.08 | 1.78 | 1392.24 | 1804.36 | 1.0 | 0.01 | 0.01 |
5131 rows × 9 columns
